package ventanas;

import isi_ib.historico_ib;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.DefaultComboBoxModel;

import com.ib.client.Contract;
import com.ib.client.EClientSocket;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Vhistorico extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private historico_ib historico;
	private JTextField tendDateTime;
	private JTextField tdurationStr_1;
	private JComboBox cdurationStr_2;
	private JTextField ttickerId;
	private JComboBox cbarSizeSetting;
	private EClientSocket   client_s;
	private Alerta vAlerta;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Vhistorico dialog = new Vhistorico(new historico_ib(),new Contract(),null,null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Vhistorico(historico_ib historico_,Contract contrato_,EClientSocket   client_s_,Alerta vAlerta_) {
		setBounds(100, 100, 352, 241);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		
		
		JLabel label = new JLabel("whatToShow");
		label.setBounds(10, 110, 113, 14);
		contentPanel.add(label);
		
		JComboBox cwhatToShow = new JComboBox();
		cwhatToShow.setModel(new DefaultComboBoxModel(new String[] {"TRADES", "MIDPOINT", "BID", "ASK", "BID_ASK", "HISTORICAL_VOLATILITY", "OPTION_IMPLIED_VOLATILITY"}));
		cwhatToShow.setSelectedIndex(0);
		cwhatToShow.setToolTipText("");
		cwhatToShow.setBounds(133, 104, 86, 20);
		contentPanel.add(cwhatToShow);
		
		JLabel label_1 = new JLabel("endDateTime(YYYYMMdd  hh:mm:ss)");
		label_1.setBounds(10, 141, 183, 14);
		contentPanel.add(label_1);
		
		tendDateTime = new JTextField();
		tendDateTime.setText("20120412 00:26:28 CET");
		tendDateTime.setColumns(10);
		tendDateTime.setBounds(203, 135, 131, 20);
		contentPanel.add(tendDateTime);
		
		JLabel label_2 = new JLabel("barSizeSetting");
		label_2.setBounds(10, 79, 113, 14);
		contentPanel.add(label_2);
		
		cbarSizeSetting = new JComboBox();
		cbarSizeSetting.setModel(new DefaultComboBoxModel(new String[] {"1 sec", "5 secs", "15 secs", "30 secs", "1 min", "2 mins", "3 mins", "5 mins", "15 mins", "30 mins", "1 hour", "1 day"}));
		cbarSizeSetting.setSelectedIndex(7);
		cbarSizeSetting.setToolTipText("");
		cbarSizeSetting.setBounds(133, 73, 86, 20);
		contentPanel.add(cbarSizeSetting);
		
		JLabel label_3 = new JLabel("durationStr");
		label_3.setBounds(10, 48, 113, 14);
		contentPanel.add(label_3);
		
		tdurationStr_1 = new JTextField();
		tdurationStr_1.setText("1");
		tdurationStr_1.setColumns(10);
		tdurationStr_1.setBounds(133, 42, 42, 20);
		contentPanel.add(tdurationStr_1);
		
		cdurationStr_2 = new JComboBox();
		cdurationStr_2.setModel(new DefaultComboBoxModel(new String[] {"S", "D", "W", "M", "Y"}));
		cdurationStr_2.setSelectedIndex(2);
		cdurationStr_2.setBounds(175, 42, 44, 20);
		contentPanel.add(cdurationStr_2);
		
		ttickerId = new JTextField();
		ttickerId.setColumns(10);
		ttickerId.setBounds(133, 11, 86, 20);
		contentPanel.add(ttickerId);
		
		JLabel label_4 = new JLabel("tickerId");
		label_4.setBounds(10, 17, 113, 14);
		contentPanel.add(label_4);
		/*
		 * 
		 * 
		 */
		historico=historico_;
		historico.setContract(contrato_);
		client_s=client_s_;
		vAlerta=vAlerta_;
		/*
		 * 
		 * 
		 * 
		 */
		
		
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		
		JButton okButton = new JButton("Inicia");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
								
				iniciaHistorico();
				
				 				 
			}
		});
		okButton.setActionCommand("OK");
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);
		
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		cancelButton.setActionCommand("Cancel");
		buttonPane.add(cancelButton);
		
		
	}
	
	private void asignaValores(){
		// historico.setContract(ccontrato.contrato);
		 historico.setEndDateTime(tendDateTime.getText());
		 historico.setDurationStr(tdurationStr_1.getText(),cdurationStr_2.getSelectedItem().toString());
		 historico.setBarSizeSetting(cbarSizeSetting.getSelectedItem().toString());
		 
		 
	}
	
	private void iniciaHistorico(){
		// historico.setContract(ccontrato.contrato);
		 
		asignaValores();
		 historico.registra();
		 historico.iniciaHistorico(client_s);
		 
		 vAlerta.anadeMensaje("HISTORICO");
		 vAlerta.anadeMensaje(historico.toString());
		 vAlerta.anadeMensaje("CONTRATO");
		 vAlerta.anadeMensaje(historico.getContract().toString());				 
		 vAlerta.anadeMensaje("client_s");
		 vAlerta.anadeMensaje(client_s.toString());
		 
	}
	
	
}
